package com.scau.database.service.shopowner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scau.data.entity.*;
import com.scau.database.common.service.ImportMapperService;
import com.scau.database.entity.shopowner.ShopOwnerQuery;
import com.scau.database.entity.shopowner.ShopOwnerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/5/9 19:55
 */
@Service
public class ShopOwnerService {

    @Autowired
    public ImportMapperService importMapperService;

    public List<ShopOwnerVO> getStores(String barCode) {
        List<ShopOwnerVO> VOS = importMapperService.stockMapper.getStores(barCode);
        return VOS;
    }

    public List<ShopOwnerVO> getStocks(String commodity) {
        List<ShopOwnerVO> VOS = importMapperService.stockMapper.getStocks(commodity);
        return VOS;
    }

    public Integer commitShelfAddedApply(ShopOwnerQuery query) {
        // query中含有商品条形码、上架数量、仓库id、原价、特价、是否特价、消费者三包期限、商店id
        // 首先提交申请，需要商店id、条形码、数量、仓库id
        // 然后上传在售物品情况，商店id、条形码、原价、特价、是否特价、
        // 剩余0个（上架申请通过后变为总共申请的数目）、
        // 总共申请多少、三包天数
        ShelfApplyDO shelfApplyDO = new ShelfApplyDO();
        shelfApplyDO.setBarCode(query.getBarCode());
        shelfApplyDO.setCount(query.getCount());
        shelfApplyDO.setStoreId(query.getStoreId());
        shelfApplyDO.setWarehouseId(query.getWarehouseId());

        OnSellDO onSellDO = new OnSellDO();
        onSellDO.setStoreId(query.getStoreId());
        onSellDO.setBarCode(query.getBarCode());
        onSellDO.setOriginalPrice(query.getOriginalPrice());
        onSellDO.setSpecialPrice(query.getSpecialPrice());
        onSellDO.setUseSpecialPrice(query.getUseSpecialPrice());
        onSellDO.setSurplus(0);
        onSellDO.setCount(query.getCount());
        onSellDO.setRepairDays(query.getRepairDays());
        onSellDO.setReturnDays(query.getReturnDays());
        onSellDO.setExchangeDays(query.getExchangeDays());

        Integer shelfResult = importMapperService.shelfApplyMapper.insert(shelfApplyDO);
        Integer onSellResult = importMapperService.onsellMapper.insert(onSellDO);
        return shelfResult & onSellResult;
    }

    public List<ShopOwnerVO> searchGoodsByStoreIdAndCommodity(ShopOwnerQuery query) {
        // 通过商店id、商品名
        // 搜索本店在售相关商品的商品名、原价特价是否特价、剩余数量、商品条形码
        // 使用onsell和orderline使用barcode连接，然后WHERE筛选本店的商品
        // 返回商品名、原价、特价、是否使用特价、剩余数量、条形码
        List<ShopOwnerVO> VOS = importMapperService.onsellMapper.searchGoodsByStoreIdAndCommodity(query);
        return VOS;
    }

    public Integer shelfDownGoods(ShopOwnerQuery shopOwnerQuery) {
        // 通过商店id、商品条形码，下架本店在售相关商品
        // 根据商店id、商品条形码从onsell删除相关行
        // 同时把ShelfOK的行移到ShelfOver中
        // 移除Onsell
        LambdaQueryWrapper<OnSellDO> deletequery = Wrappers.<OnSellDO>lambdaQuery()
                .eq(OnSellDO::getStoreId, shopOwnerQuery.getStoreId())
                .eq(OnSellDO::getBarCode, shopOwnerQuery.getBarCode());
        importMapperService.onsellMapper.delete(deletequery);
        // 获取ShelfOK并赋值给ShelfOver
        LambdaQueryWrapper<ShelfOKDO> searchQuery = Wrappers.<ShelfOKDO>lambdaQuery()
                .eq(ShelfOKDO::getStoreId, shopOwnerQuery.getStoreId())
                .eq(ShelfOKDO::getBarCode, shopOwnerQuery.getBarCode())
                .last(" limit 1");
        ShelfOKDO OkDO = importMapperService.shelfOKMapper.selectOne(searchQuery);
        // 移除ShelfOK
        LambdaQueryWrapper<ShelfOKDO> deleteQuery = Wrappers.<ShelfOKDO>lambdaQuery()
                .eq(ShelfOKDO::getStoreId, shopOwnerQuery.getStoreId())
                .eq(ShelfOKDO::getBarCode, shopOwnerQuery.getBarCode());
        importMapperService.shelfOKMapper.delete(deleteQuery);
        // 移入ShelfOver
        ShelfOverDO overDO = new ShelfOverDO();
        overDO.setBarCode(OkDO.getBarCode());
        overDO.setCount(OkDO.getCount());
        overDO.setId(OkDO.getId());
        overDO.setStoreId(OkDO.getStoreId());
        overDO.setSurplus(OkDO.getSurplus());
        overDO.setWarehouseId(OkDO.getWarehouseId());
        Integer result = importMapperService.shelfOverMapper.insert(overDO);
        return result;
    }

}
